import tkinter as tk
from tkinter import messagebox

def center_window(win, width=300, height=180):
    screen_width = win.winfo_screenwidth()
    screen_height = win.winfo_screenheight()
    x = (screen_width - width) // 2
    y = (screen_height - height) // 2
    win.geometry(f"{width}x{height}+{x}+{y}")

def add_user_window():
    win = tk.Toplevel()
    win.title("添加会员")
    center_window(win)  # 居中弹窗
    win.grab_set()

    frame = tk.Frame(win)
    frame.pack(padx=20, pady=20, fill='both', expand=True)

    tk.Label(frame, text="姓名:").grid(row=0, column=0, sticky="e", pady=5)
    name_entry = tk.Entry(frame)
    name_entry.grid(row=0, column=1, pady=5)

    tk.Label(frame, text="手机号:").grid(row=1, column=0, sticky="e", pady=5)
    phone_entry = tk.Entry(frame)
    phone_entry.grid(row=1, column=1, pady=5)

    def save():
        name = name_entry.get().strip()
        phone = phone_entry.get().strip()
        if not name or not phone:
            messagebox.showerror("错误", "姓名和手机号不能为空")
            return
        print(f"保存会员：姓名={name}, 手机号={phone}")
        win.destroy()

    btn_frame = tk.Frame(win)
    btn_frame.pack(pady=10)

    tk.Button(btn_frame, text="添加", command=save, width=10).pack(side="left", padx=5)
    tk.Button(btn_frame, text="取消", command=win.destroy, width=10).pack(side="right", padx=5)

if __name__ == "__main__":
    root = tk.Tk()
    root.withdraw()
    add_user_window()
    root.mainloop()
